package com.microtf.inscription.web.controller.admin;

import com.microtf.inscription.framework.form.FormGenerateService;
import com.microtf.inscription.framework.service.BlogService;
import com.microtf.inscription.framework.vo.AjaxDto;
import com.microtf.inscription.jpa.entity.BlogSpace;
import com.microtf.inscription.jpa.entity.User;
import com.microtf.inscription.web.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

/**
 * 分类
 * @author guliuzhong
 */
@Controller
@RequestMapping("/admin/space")
@Slf4j
public class SpaceController extends BaseController {
    FormGenerateService formGenerateService;
    BlogService blogService;

    @Autowired
    public void setFormGenerateService(FormGenerateService formGenerateService) {
        this.formGenerateService = formGenerateService;
    }

    @Autowired
    public void setBlogService(BlogService blogService) {
        this.blogService = blogService;
    }

    @GetMapping("/list")
    ModelAndView list(ModelAndView modelAndView){
        Optional<User> loginUser = getLoginUser();
        List<BlogSpace> blogSpace = blogService.getBlogSpace(loginUser.get().getId());
        modelAndView.addObject("space", blogSpace);
        modelAndView.addObject("site", getSiteSetting());
        return modelAndView;
    }
//    @GetMapping("/edit/{spaceId}")
//    ModelAndView edit(ModelAndView modelAndView, @PathVariable(value = "spaceId",required = false) Integer spaceId){
//        modelAndView.addObject("site", getSiteSetting());
//        Optional<com.qintingfm.web.jpa.entity.Category> category = categoryService.getCategory(catId);
//        com.qintingfm.web.jpa.entity.Category category1 = category.orElse(new com.qintingfm.web.jpa.entity.Category());
//        CategoryVo categoryVo=new CategoryVo();
//        BeanUtils.copyProperties(category1,categoryVo);
//        Form form =  formGenerateService.generalFormData(categoryVo);
//        modelAndView.addObject("form", form);
//        modelAndView.setViewName("admin/space/edit");
//        return modelAndView;
//    }
//    @PostMapping("/edit")
//    @ResponseBody
//    AjaxDto edit(CategoryVo categoryVo){
//        com.qintingfm.web.jpa.entity.Category category1;
//        if(categoryVo.getCatId()!=null){
//            Optional<com.qintingfm.web.jpa.entity.Category> category = categoryService.getCategory(categoryVo.getCatId());
//            category1=category.orElse(new com.qintingfm.web.jpa.entity.Category());
//        }else{
//            category1=new com.qintingfm.web.jpa.entity.Category();
//        }
//        category1.setTitle(categoryVo.getTitle());
//        category1.setDescription(categoryVo.getDescription());
//        categoryService.save(category1);
//        AjaxDto ajaxDto=new AjaxDto();
//        ajaxDto.setMessage("保存成功");
//        try {
//            Method list = CategoryController.class.getDeclaredMethod("list", ModelAndView.class);
//            ajaxDto.setLink(MvcUriComponentsBuilder.fromMethod(CategoryController.class, list, new ModelAndView()).build().toUriString());
//            ajaxDto.setAutoJump(1);
//        } catch (NoSuchMethodException e) {
//            log.error("未找到页面"+e.getMessage());
//        }
//
//        return ajaxDto;
//    }
}
